package org.atlas.client.maven;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.atlas.mda.ConfigurationLoader;
import org.atlas.mda.Context;
import org.atlas.mda.ModelTransformer;
import org.atlas.mda.Output;

/**
 * @goal generate
 */
public class GenerateSourcesMojo extends AbstractMojo {

    /**
     * Configuration location
     *
     * @parameter property="configLocation"
     */
    private String configLocation;

    /**
     * Configuration location
     *
     * @parameter property="rootFolder"
     */
    private String rootFolder;

    public void setConfigLocation(String configLocation) {
        this.configLocation = configLocation;
        System.out.println(configLocation);
    }

    public void setRootFolder(String rootFolder) {
        this.rootFolder=rootFolder;
        System.out.println(rootFolder);
    }

    /**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    public void execute() throws MojoExecutionException {
        getLog().info("Executing Atlas Engine...");
        try {
            Context.reset();
            Context.setRootFolder(rootFolder);
            ConfigurationLoader.setConfigLocation(configLocation);
            ModelTransformer transformer = new ModelTransformer();
            transformer.transform();
            for(Output output:Context.getOutputs().values()) {
                this.project.addCompileSourceRoot(output.getDir());
            }
        } catch (Exception e) {
            getLog().error(e);
            throw new MojoExecutionException("Atlas Engine failed.", e);
        }
    }
}

